package com.elitesland.org.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.org.param.EmployeeTagQParam;
import com.elitesland.org.service.EmployeeTagService;
import com.elitesland.org.vo.EmployeeTagVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/org/employees/tags")
@Api(value = "组织结构", tags = {"雇员标签"})
public class EmployeeTagController {

    private EmployeeTagService employeeTagService;

    @Autowired
    public void setEmployeeTagService(EmployeeTagService employeeTagService) {
        this.employeeTagService = employeeTagService;
    }

    @PostMapping("/q")
    @ApiOperation("分页检索标签")
    public ApiResult<?> search(@RequestBody EmployeeTagQParam param) {
        return ApiResult.ok(
                employeeTagService.search(param)
        );
    }

    @GetMapping("/{tagname}")
    @ApiOperation("根据标签返回标签对象，含对应雇员集合")
    public ApiResult<?> oneByTagName(@PathVariable String tagname) {
        return employeeTagService.oneByTagName(tagname).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @PostMapping
    @ApiOperation("创建标签")
    public ApiResult<?> create(@RequestBody EmployeeTagVO tag) {
        return ApiResult.ok(
                employeeTagService.create(tag)
        );
    }

    @PutMapping
    @ApiOperation("更新标签")
    public ApiResult<?> update(@RequestBody EmployeeTagVO tag) {
        employeeTagService.update(tag);
        return ApiResult.ok();
    }

    @DeleteMapping
    @ApiOperation("批量删除标签")
    public ApiResult<?> removeByIds(@RequestBody List<Long> ids) {
        employeeTagService.removeByIds(ids);
        return ApiResult.ok();
    }

    @PutMapping("/{tagname}")
    @ApiOperation("将多个雇员记录ID绑定到标签")
    public ApiResult<?> bindEmployeeIdsToTag(
            @PathVariable String tagname,
            @RequestBody List<Long> employeeIds) {
        employeeTagService.bindEmployeesToTag(employeeIds, tagname);
        return ApiResult.ok();
    }

    @PutMapping("/byemployee/{employeeid}")
    @ApiOperation("将多个标签绑定到雇员记录ID")
    public ApiResult<?> bindEmployeeIdsToTag(
            @PathVariable Long employeeid,
            @RequestBody List<String> tags) {
        employeeTagService.bindTagsToEmployee(tags, employeeid);
        return ApiResult.ok();
    }
}
